package cn.zd.demo1;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import cn.zd.demo1.util.ViewCheckUtil;

public class MyApplicationActivity extends AppCompatActivity implements View.OnClickListener {

    private TextView tv_1;
    private EditText et_1;

    private MyApplication app;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_myapplication);
        Toast.makeText(this,this.getClass().getName(),Toast.LENGTH_LONG).show();
        app = MyApplication.getInstance();
        TextView tv_0 = findViewById(R.id.tv_0);
        tv_1 = findViewById(R.id.tv_1);
        et_1 = findViewById(R.id.et_1);
        findViewById(R.id.btn_1).setOnClickListener(this);
        findViewById(R.id.btn_2).setOnClickListener(this);
        findViewById(R.id.btn_0).setOnClickListener(this);
        Object saveVal = app.dataMap.get(MyApplication.TEST_KEY);
        et_1.setText(saveVal == null? "":saveVal.toString());
        tv_0.setText("应用启动时间:"+app.dataMap.get("start"));
    }

    @Override
    public void onClick(View v) {
        if(v.getId() == R.id.btn_0) {
            app.dataMap.remove(MyApplication.TEST_KEY);
        }else if(v.getId() == R.id.btn_1) {
            if(!ViewCheckUtil.checkEditText(et_1)) {
                Toast.makeText(this,"请输入任意数据",Toast.LENGTH_SHORT).show();
                et_1.requestFocus();
            }
            app.dataMap.put(MyApplication.TEST_KEY,et_1.getText().toString());
            Toast.makeText(this,"保存成功",Toast.LENGTH_SHORT).show();
            tv_1.setText(app.dataMap.get("str1").toString());
        } else if(v.getId() == R.id.btn_2) {
            startActivity(new Intent(this,MyApplicationNextActivity.class));
        }
    }
}